<?php
/**
 * Created by PhpStorm.
 * User: YAN
 * Date: 2017/7/24
 * Time: 15:10
 */

namespace App\Http\Controllers\API;


use App\Components\GetUserInfo;
use App\Components\getUsersWeiXinInfo;
use App\Http\Controllers\Controller;
use App\Models\ElectiveStudent;
use App\Models\Semester;
use App\Models\Student;
use App\Models\StudentGroup;
use App\Services\Platform;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class LoginController extends Controller
{

    public function index(Request $request)
    {
        $state = $request->get('state');
        $code = $request->get('code');

        //   Log::info(dd($request));
        $info = new getUsersWeiXinInfo();
        $opens = $info->getInfo($code);
        $open = \GuzzleHttp\json_decode($opens);
        if (empty($open->openid)) {
            exit('产生错误');
        }
        $user_code = \Platform::checkStudent($open->openid);
        $userbangding = Student::query()->where('wx_key',$open->openid)->first();
        if (!$user_code || !$userbangding) {
            return redirect(env('HOME_URL') . '/weiui#/accountFailed/' . $open->openid);
        }
        $student = Student::query()->where(['u_code' => $user_code->student_code])->first();
        if (!$student) {
            return redirect(env('HOME_URL') . '/weiui#/accountFailed/' . $open->openid);
        }
        switch ($state) {
            case 'accountSuccess';
                return redirect(env('HOME_URL') . '/weiui#/accountSuccess/' . $open->openid);
                break;
            case 'classBox';
                $time = $this->checkTime();
                if ($time) {
                    return redirect(env('HOME_URL') . '/weiui#/classBox/' . $open->openid . '/' . $time['semester'] . '/' . $time['week']);
                } else {
                    return redirect(env('HOME_URL') . '/weiui#/classBox/' . $open->openid . '/0/1');
                }
                break;
            case 'examScoreQuery';
                return redirect(env('HOME_URL') . '/weiui#/examScoreQuery/' . $open->openid . '/1');
                break;
            case 'examQuery';
                return redirect(env('HOME_URL') . '/weiui#/examQuery/' . $open->openid . '/1');
                break;
            case 'examAllTerms';
                return redirect(env('HOME_URL') . '/weiui#/examAllTerms/' . $open->openid . '/1');
                break;
            case 'cetQuery';// 四六级成绩查询
                return redirect(env('HOME_URL') . '/weiui#/cetQuery/' . $open->openid . '/1');
                break;
            case 'cetScore';// 四六级成绩结果
                return redirect(env('HOME_URL') . '/weiui#/cetScore/' . $open->openid);
                break;
            case 'accountRelease';
                return redirect(env('HOME_URL') . '/weiui#/accountRelease/' . $open->openid);
                break;
            case 'personalCenter';
                return redirect(env('HOME_URL') . '/weiui#/personalCenter/' . $open->openid);
                break;
            case 'courseGroupTip';
                // exit('选课已结束');
                $res = $this->checkSelect($open->openid);
                switch ($res) {
                    case 0;
                        return redirect(env('HOME_URL') . '/weiui#/courseGroup/' . $open->openid);
                        break;
                    case 1;
                        return redirect(env('HOME_URL') . '/weiui#/courseGroupTip/' . $open->openid);
                        break;
                    default;
                        return redirect(env('HOME_URL') . '/weiui#/courseGroupResult/' . $open->openid);
                        break;
                }
            case 'courseGroupResult';
                //  exit('选课已结束');
                return redirect(env('HOME_URL') . '/weiui#/courseGroupResult/' . $open->openid);
                break;
            case 'curriculaTip';
                return redirect(env('HOME_URL') . '/weiui#/curriculaTip/' . $open->openid);
                break;
            case 'curriculaResult';
                return redirect(env('HOME_URL') . '/weiui#/curriculaResult/' . $open->openid);
                break;
            case 'professional';
                return redirect(env('HOME_URL') . '/weiui#/authentication/' . $open->openid);
                break;
            case 'user-confirm';
                return redirect(env('HOME_URL') . '/weiui#/user-confirm/' . $open->openid);
                break;
            default;
                return redirect(env('HOME_URL') . '/weiui#/accountSuccess/' . $open->openid);
        }
    }

    public function checkTime()
    {
        $time = time();
        $semester = Semester::query()->where('times_temp', '<', $time)->orderBy('times_temp', 'desc')->first();
        if ($semester) {
            $weeks = ceil(($time - $semester->times_temp) / (3600 * 24 * 7));
            if ($weeks <= $semester->s_week) {
                return ['week' => $weeks, 'semester' => $semester->id];
            }
        }
        return false;
    }

    public function checkSelect($open_id)
    {

        $student = Student::query()->where(['wx_key' => $open_id])->first();
        if (!$student) {
            return 0;
        }
        $c_student = ElectiveStudent::query()->where(['student_code' => $student->u_code])->first();
        if (!$c_student) {
            return 0;
        }

        return 1;

    }
}
